import { defineConfig, loadEnv } from 'vite'
import ElementPlus from 'unplugin-element-plus/vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import resolveExternalsPlugin from 'vite-plugin-resolve-externals'
export default ({ mode }) => {
  return defineConfig({
    server: {
      host: '0.0.0.0',
      port: 8083,
      open: true,
      proxy: {
        '/api': {
          target: 'https://nxqmrhtest.hnhuicha.com',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, '')
        }
      }
    },
    resolve: {
      alias: [
        {
          find: '@',
          replacement: resolve(__dirname, 'src')
        }
      ]
    },
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove();
                }
              }
            }
          }
        ],
      },
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/styles/element.scss" as *; 
        @use "@/styles/variable.scss" as * ; 
        @use "@/styles/mixins.scss" as * ;`,
        },
      },
    },
    plugins: [
      vue(),
      ElementPlus({
        useSource: true
      }),
      Components({
        extensions: ['vue', 'md'],
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
        resolvers: [
          ElementPlusResolver({
            importStyle: 'sass',
          }),
        ],
        dts: 'src/components.d.ts',
      }),
      // resolveExternalsPlugin({
      //   AMap: 'AMap' // 这个名字可以直接打印window，看window上挂的是什么名字，就写什么名字
      // })
    ],
    base: loadEnv(mode, process.cwd()).VITE_WEB_API ? './' : '/',
    build: {
      outDir: loadEnv(mode, process.cwd()).VITE_WEB_API === 'test' ? 'dev' : 'dist',//指定输出路径（相对于 项目根目录).
    },
  })
}
